#include "player_statistics.h"
#include "gamestatistic.h"
#include "dbconnect.h"
#include <QtSql>

PlayerStatistics::PlayerStatistics(){
    gameStats = new QVector<GameStatistic*>();
    gamesPlayed=0;
    gamesPitched=0;
}

PlayerStatistics::~PlayerStatistics(){
}

QVector<GameStatistic*>* PlayerStatistics::getGameStatistics(){
    return gameStats;
}

GameStatistic PlayerStatistics::getPriorHistory(){
    return priorHistory;
}

void PlayerStatistics::addGameStatistic(GameStatistic* gs){
    gameStats->append(gs);
}

void PlayerStatistics::setPriorHistory(GameStatistic* gs){
    priorHistory=*gs;
}


int PlayerStatistics::getGamesPlayed(){
    return gamesPlayed;
}

void PlayerStatistics::setGamesPlayed(int inputGamesPlayed){
    gamesPlayed=inputGamesPlayed;
}

void PlayerStatistics::incrementGamesPlayed(){
    gamesPlayed++;
}

int PlayerStatistics::getGamesPitched(){
    return gamesPitched;
}

void PlayerStatistics::setGamesPitched(int inputGamesPitched){
    gamesPitched=inputGamesPitched;
}

void PlayerStatistics::incrementGamesPitched(){
    gamesPitched++;
}

void PlayerStatistics::setTotals(GameStatistic *gs){
    totals=*gs;
}

void PlayerStatistics::pushLastGameToDB(int player_id, int game_id) {

    GameStatistic* last_game_stats = gameStats->last();  // I hope this pulls last stats in the vector.  No way to check it tho.  My simulator has only few players and works super slow
                                                       // So i cant go through the whole game.

    DbConnect db = DbConnect();
    QSqlQuery query;


    QString insert_stats = QString("INSERT INTO player_stats VALUES(%1 , %2, %3, %4, %5, %6 , %7 , %8 , %9 , %10 , %11 , %12 , %13 , %14 ,  %15 ,  %16 , %17 ,  %18 ,  %19 ,  %20 ,  %21 ,  %22 ,  %23)")
            .arg(last_game_stats->getAtBats())
            .arg(last_game_stats->getSingles())
            .arg(last_game_stats->getDoubles())
            .arg(last_game_stats->getTriples())
            .arg(last_game_stats->getGrandSlams())
            .arg(last_game_stats->getHomeRuns())
            .arg(last_game_stats->getWalks())
            .arg(last_game_stats->getStrikeouts())
            .arg(last_game_stats->getRunsScored())
            .arg(last_game_stats->getRunsBattedIn())
            .arg(last_game_stats->getStolenBases())
            .arg("0")
            .arg(last_game_stats->getWalksPitched())
            .arg(last_game_stats->getEarnedRuns())
            .arg(last_game_stats->getInningsPitched())
            .arg(last_game_stats->getStrikeoutsPitched())
            .arg(last_game_stats->getWins())
            .arg(last_game_stats->getLosses())
            .arg(last_game_stats->getHitsAllowed())
            .arg(last_game_stats->getAtBatsFaced())
            .arg(last_game_stats->getErrors())
            .arg(game_id) // 0 represents History stats
            .arg(player_id);
    query.prepare(insert_stats);
    query.exec();

    qDebug() << insert_stats;

}
